using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class KhachHangDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveKhachHang(KhachHang khachhang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddKhachHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddKhachHang";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, khachhang.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@MaSoKhachHang", DbType.String, ParameterDirection.Input, khachhang.MaSoKhachHang);
            cmd.Parameters.Add(param);
			param = CreateParameter("@Ten", DbType.String, ParameterDirection.Input, khachhang.Ten);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaSoThue", DbType.String, ParameterDirection.Input, khachhang.MaSoThue);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DiaChi", DbType.String, ParameterDirection.Input, khachhang.DiaChi);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DienThoai", DbType.String, ParameterDirection.Input, khachhang.DienThoai);
			cmd.Parameters.Add(param);
			param = CreateParameter("@Fax", DbType.String, ParameterDirection.Input, khachhang.Fax);
			cmd.Parameters.Add(param);
			param = CreateParameter("@Email", DbType.String, ParameterDirection.Input, khachhang.Email);
			cmd.Parameters.Add(param);
			param = CreateParameter("@Website", DbType.String, ParameterDirection.Input, khachhang.Website);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ChietKhau", DbType.Double, ParameterDirection.Input, khachhang.ChietKhau);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all KhachHang in DB
		public ResultObj GetAllKhachHang( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<KhachHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKhachHang", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<KhachHang>();
			KhachHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new KhachHang();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.MaSoKhachHang = Convert.ToString(reader["MaSoKhachHang"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.MaSoThue = Convert.ToString(reader["MaSoThue"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.DienThoai = Convert.ToString(reader["DienThoai"]);
			item.Fax = Convert.ToString(reader["Fax"]);
			item.Email = Convert.ToString(reader["Email"]);
			item.Website = Convert.ToString(reader["Website"]);
			item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all KhachHang in DB with condition
		public ResultObj GetAllKhachHangByID(KhachHang khachhang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<KhachHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKhachHangByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, khachhang.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<KhachHang>();
			KhachHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new KhachHang();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.MaSoKhachHang = Convert.ToString(reader["MaSoKhachHang"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.MaSoThue = Convert.ToString(reader["MaSoThue"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.DienThoai = Convert.ToString(reader["DienThoai"]);
			item.Fax = Convert.ToString(reader["Fax"]);
			item.Email = Convert.ToString(reader["Email"]);
			item.Website = Convert.ToString(reader["Website"]);
			item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a KhachHang out of DB with condition
		public ResultObj DeleteKhachHang(KhachHang khachhang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteKhachHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteKhachHang";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, khachhang.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of ChiNhanhKhachHang from DB with condition
		public ResultObj GetChiNhanhKhachHang(int IdChiNhanhKhachHang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<ChiNhanhKhachHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetChiNhanhKhachHangByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdChiNhanhKhachHang);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<ChiNhanhKhachHang>();
			ChiNhanhKhachHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new ChiNhanhKhachHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdKhachHang = Convert.ToInt32(reader["IdKhachHang"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.MaSoThue = Convert.ToString(reader["MaSoThue"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.DienThoai = Convert.ToString(reader["DienThoai"]);
			item.Fax = Convert.ToString(reader["Fax"]);
			item.Email = Convert.ToString(reader["Email"]);
			item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
